<?php
/**
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

namespace SDXApi\Service;

/**
 * 金蝶相关
 * Created by PhpStorm.
 * User: poptser@foxmail.com
 * Date: 2018/3/27
 * Time: 13:53
 */
class KingdeeService {

    //public $serverUrl = "http://api.jd.shidexian.com";  //服务器地址
    public $serverUrl = "http://172.18.2.120";          //服务器内网IP

    public function __construct()
    {

    }

    /**
     * 收货确认操作回仓  执行退货退款
     * Author:poptser@foxmail.com
     * @param $orderId
     * @return mixed
     */
    public function BackToWarehouseGoods($orderId,$wid){
        $KindeeUrl = $this->serverUrl."/warehousemange/salesrefunds";
        $goodsListForKingdee['refund_type'] = 1;
        $goodsListForKingdee['wid'] = $wid;
        $goodsArray = array();
        $del_discount = 0;
        $goodsList = M("order_goods")->where(array("order_id"=>$orderId))->select();
        foreach ($goodsList as $value){
            if($value['goods_number']>$value['refund_number']){
                $goodsInfo = M('goods')->where(array('goods_id' =>$value['goods_id']))->find();
                $goodsArray[] = array(
                    "goods_sn"=>$value['goods_sn'],
                    "spec_unit"=>$goodsInfo['spec_unit'],   //规格
                    "goods_number"=>$value['goods_number']-$value['refund_number'],
                    "k3_supplier_sn"=>$goodsInfo['supplier_code'],    //供应商编号
                    "k3_vmi"=>$goodsInfo['consignment'],             //是否代销
                    "goods_price"=>$value['goods_price']             //含税单价
                );
                $del_discount += $value['discount_price'] * ($value['goods_number']-$value['refund_number']);
            }
        }
        $goodsListForKingdee['goodslist'] = $goodsArray;

        $order_info = M("order_info")->where(array("order_id"=>$orderId))->field("discount,order_sn,shipping_fee,bonus_freight_id")->find();

        $goodsListForKingdee['order_sn'] = $order_info['order_sn'];
        $goodsListForKingdee['k3_number'] = M("warehouse")->where(array("id"=>$wid))->getField("k3_number");
        if($order_info['bonus_freight_id']==0){
            $goodsListForKingdee['freight'] = $order_info['shipping_fee'];    //运费;
        }else{
            $goodsListForKingdee['freight'] = 0;    //运费;
        }

        $goodsListForKingdee['discountamount'] = $order_info['discount']-$del_discount;    //优惠金额;
        //dump();
        $postData = array();
        $postData['refund_data'] = json_encode($goodsListForKingdee);
        //dump(json_encode($goodsListForKingdee));
        return $this->rui_http_post($KindeeUrl,$postData);
    }


    /**
     * 审核发货通知单 【 质检操作 】
     * Author:poptser@foxmail.com
     * @param $orderId;
     * @return mixed
     */
    public function checkSalesOrder($orderId){
        if(empty($orderId)){
            return false;
        }
        $url = $this->serverUrl."/warehousemange/submitconsignmentnote";

        $data['k3cn_sn'] = M("order_info")->where(array("order_id"=>$orderId))->getField("k3cn_sn");
        if(!empty($data['k3cn_sn'])){
            return $this->rui_http_post($url,$data);
        }else{
            $KingdeeArray['code'] = 200;
            $KingdeeArray['data']['Number'] = '非金蝶订单';
            return json_encode($KingdeeArray);
        }

    }


    /**
     * 销售单转为出库单 【 出仓操作 】
     * Author:poptser@foxmail.com
     * @param $orderIdArray
     * @return mixed
     */
    public function changeToOutbound($orderId){
        $KingdeeOrderSnArray = M("order_info")->where(array("order_id"=>$orderId))
            ->getField("k3cn_sn");
        $url = $this->serverUrl."/warehousemange/consignmentnotepushsalesrelease";
        $data['numbers'] = json_encode($KingdeeOrderSnArray);
        if(!empty($KingdeeOrderSnArray)){
            return $this->rui_http_post($url,$data);
        }else{
            $KingdeeArray['code'] = 200;
            $KingdeeArray['data']['Number'] = '非金蝶订单';
            return json_encode($KingdeeArray);
        }

    }


    /**
     * http post
     * Author:poptser@foxmail.com
     * @param $url
     * @param $param
     * @return bool|mixed
     */
    private function rui_http_post($url, $param){
        $post_file=false;
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach($param as $key=>$val){
                $aPOST[] = $key."=".urlencode($val);
            }
            $strPOST =  join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
//        $res = json_decode($sContent,true);
//        if($res['error']){
        return $sContent;

    }
}